package com.vclee.fast.integration.power;

import com.vclee.fast.integration.power.model.SysRole;
import com.vclee.fast.integration.power.service.SysRoleService;
import com.vclee.fast.utils.aspect.SysLog;
import com.vclee.fast.utils.exception.Assert;
import com.vclee.fast.utils.exception.R;
import com.vclee.fast.utils.model.Bson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/admin-sysRole")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;

    @SysLog("后台添加系统角色")
    @PostMapping(value = "/addRole")
    public R addRole(@RequestBody SysRole sysRole){
        sysRoleService.addRole(sysRole);
        return R.ok(sysRole);
    }

    @SysLog("后台修改系统角色")
    @PostMapping(value = "/editRole")
    public R editRole(@RequestBody SysRole sysRole){
        sysRoleService.editRole(sysRole);
        return R.ok();
    }

    @SysLog("后台删除系统角色")
    @PostMapping(value = "/delRole")
    public R delRole(@RequestBody SysRole sysRole){
        sysRoleService.delRole(sysRole);
        return R.ok();
    }

    @SysLog("后台查询系统角色列表")
    @PostMapping(value = "/findListByQuery")
    public R findListByQuery(@RequestBody Bson queryParams){
        return R.ok(sysRoleService.findListByQuery(queryParams));
    }

    @SysLog("后台查询系统角色菜单列表")
    @PostMapping(value = "/findRoleMenuList")
    public R findRoleMenuList(@RequestBody Bson query){
        return R.ok(sysRoleService.findRoleMenuList(query.getInteger("roleId")));
    }

    @SysLog("后台编辑角色菜单")
    @PostMapping(value = "/addRoleMenu")
    public R addRoleMenu(@RequestBody Bson query){
        List<Integer> list= new ArrayList<>();
        String menuIds = query.getString("menuIds");
        if(Assert.notBlank(menuIds)){
            list = Arrays.stream(menuIds.split(","))
                   .map(Integer::parseInt).collect(Collectors.toList());
        }
        Integer roleId = query.getInteger("roleId");
        sysRoleService.addRoleMenu(roleId,list);
        return R.ok();
    }
}
